package com.hezhou.oauth2.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hezhou.oauth2.web.entities.SysPermission;
import com.hezhou.oauth2.web.mapper.SysPermissionMapper;
import com.hezhou.oauth2.web.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ---------------------------
 * (SysPermissionServiceImpl) 系统权限逻辑层实现
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/3/6
 * @Version: [1.0.1]
 * ---------------------------
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<SysPermission> findPermissByUserId(Long userId) {
        if (userId == null){
            return null;
        }
        List<SysPermission> sysPermissions = sysPermissionMapper.selectPermissionByUserId(userId);
        sysPermissions.remove(null); //如果集合中没有权限，则将集合中的null数据移除掉
        return sysPermissions;
    }

    /**
     * 根据手机号查询登陆权限信息
     * @param mobile
     * @return
     */
    @Override
    public List<SysPermission> findPermissByMobile(String mobile) {
        if (mobile == null){
            return null;
        }
        List<SysPermission> sysPermissions = sysPermissionMapper.selectPermissionByMobile(mobile);
        return sysPermissions;
    }

    /**
     * 根据id删除权限信息  包括他下边的子权限
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Boolean deleteById(Long id) {
        //删除当前id的权限
        baseMapper.deleteById(id);
        //删除子权限
        LambdaQueryWrapper<SysPermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //delete from sys_permission where parent_id = id
        lambdaQueryWrapper.eq(SysPermission::getParentId,id);
        baseMapper.delete(lambdaQueryWrapper);
        return true;
    }
}
